package com.itheima.health.util;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @Author: xuwendong
 * @Date: 2021/7/16 15:05
 * @Description: 阿里云短信发送工具类
 */
@Slf4j

public class AliyunSmsUtil {
    //短信发送成功状态码
    private static final String OK = "OK";
    //阿里云短信发送相关参数设置
    private static final String REGIONID = "cn-beijing";
    private static final String DOMAIN = "dysmsapi.aliyuncs.com";
    private static final String VERSION = "2017-05-25";
    private static final String ACTION = "SendSms";
    //
    private static final String accessKeyId = "LTAI5tB1LoEj1xjc4RWjY7Go";
    private static final String accessKeySecret = "6d8LITb4j1NWiZ3C9MtFo08M5HovN4";

    /**
     * 功能描述: 阿里云短信发送接口，支持通知、验证码、激活码类短信
     *
     * @param map map中所需参数，参数名称固定，如下(注意：参数首字母都是小写的)：
     *            phoneNumbers ： 手机号码，支持多个中间以“,”英文逗号分隔 （必填）
     *            signName ：签名（必填）
     *            templateCode ：模板code（必填）
     *            templateParam ：模板参数（不必填，如果是通知类短信，该参数可以不设置）
     * @return : java.lang.Boolean
     * true-短信发送成功；false-短信发送失败
     */
    public static String  sendSms(Map<String, String> map,String authCode) {
        Boolean flag = false;
        DefaultProfile profile = DefaultProfile.getProfile(REGIONID, accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        //发送短信域名，不要改变
        request.setSysDomain(DOMAIN);
        request.setSysVersion(VERSION);
        request.setSysAction(ACTION);
        request.putQueryParameter("PhoneNumbers", map.get("phoneNumbers"));
        System.out.println(map.get("phoneNumbers"));
        request.putQueryParameter("SignName", map.get("signName"));
        request.putQueryParameter("TemplateCode", map.get("templateCode"));
        //通知类短信，模板不需要参数。判断传递的模板参数是否为空，如果为空可能是通知类短信，则不需要添加模板参数条件
        if (authCode != null) {
            request.putQueryParameter("TemplateParam", "{\"code\":\"" + authCode + "\"}");
        }
        try {
            //发送短信
            CommonResponse response = client.getCommonResponse(request);
            //发送短信结果转为Map类型
            Map<String, String> responseMap = JSON.parseObject(response.getData(), Map.class);
            //判断短信发送是否成功
            if (OK.equals(responseMap.get("Code"))) {
                flag = true;
            }
            log.debug("send fail[code={}, message={}]", responseMap.get("Code"), responseMap.get("Message"));
            log.info("send fail[code={}, message={}]", responseMap.get("Code"), responseMap.get("Message"));
            log.info("你的验证码是："+authCode);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return authCode;
    }

}
